home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / make / main.c < prev    next >
C/C++ Source or Header  |  1990-07-15  |  7KB  |  290 lines

  1. /*************************************************************************
  2.  *
  3.  *  m a k e :   m a i n . c
  4.  *
  5.  *========================================================================
  6.  * Edition history
  7.  *
  8.  *  #    Date                         Comments                       By
  9.  * --- -------- ---------------------------------------------------- ---
  10.  *   1    ??                                                         ??
  11.  *   2 01.07.89 strcmp(makefile,..) only if makefile a valid ptr.    RAL
  12.  *   3 23.08.89 initname() added                                     RAL
  13.  *   4 30.08.89 argument parsing impr., indention ch., macro fl. add.PSH,RAL
  14.  *   5 03.09.89 k-option added, initname -> init changed             RAL
  15.  *   6 06.09.89 environment, MAKEFLAGS, e,d,a options added,         RAL
  16.  *   7 09.09.89 tos support added, fatal args added, fopen makefile  PHH,RAL
  17.  *   8 17.09.89 setoptions fixed for __STDC__                        RAL
  18.  * ------------ Version 2.0 released ------------------------------- RAL
  19.  *
  20.  *************************************************************************/
  21.  
  22. /*
  23.  *    make:
  24.  *
  25.  *    -a try to guess undefined ambiguous macros (*,<)
  26.  *    -d print debugging info
  27.  *    -e environment macro def. overwrite makefile def.
  28.  *    -f makefile name
  29.  *    -i ignore exit status
  30.  *    -k continue on errors
  31.  *    -n pretend to make
  32.  *    -p print all macros & targets
  33.  *    -q question up-to-dateness of target.  Return exit status 1 if not
  34.  *    -r don't not use inbuilt rules
  35.  *    -s make silently
  36.  *    -t touch files instead of making them
  37.  *    -m Change memory requirements (EON only)
  38.  */
  39.  
  40. #define EXTERN
  41. #define INIT(x) = x
  42. #define INITARRAY
  43. #include "h.h"
  44.  
  45. static char version[]= "2.0";
  46.  
  47. static char *makefile;      /*  The make file  */
  48. static FILE *ifd;           /*  Input file desciptor  */
  49. static char *ptrmakeflags;
  50. static char  makeflags[] = "MAKEFLAGS=                    ";
  51.    /* there must be enough 'space' for all possible flags ! */
  52.  
  53.  
  54. void main(argc, argv)
  55. int    argc;
  56. char **argv;
  57. {
  58.   register char        *p;        /*  For argument processing  */
  59.   int                   estat = 0;    /*  For question  */
  60.   register struct name *np;
  61.   struct macro         *mp;
  62.   int                   targc;        /* temporary for multiple scans */
  63.   char                **targv;
  64.   char                **nargv;        /* for removing items from argv */
  65.   char                **envp;      /* enivironment ptr */
  66.  
  67.  
  68.   ptrmakeflags = &makeflags[10];
  69.   myname = (argc-- < 1) ? "make" : *argv++;
  70. #ifdef tos
  71.   myname = "Make";
  72. #endif
  73.  
  74.   targc = argc;
  75.   targv = nargv = argv;
  76.   while (targc--) {
  77.     if((p = strchr(*targv, '=')) != (char *)NULL) {
  78.         *p = '\0';
  79.         mp = setmacro(*targv, p + 1);
  80.         mp->m_flag |= M_OVERRIDE;
  81.         --argc;
  82.     } else
  83.         *nargv++ = *targv;
  84.  
  85.     ++targv;
  86.   }
  87.  
  88.   targc = argc;
  89.   targv = nargv = argv;
  90.   while (targc--) {
  91.     if (**targv == '-') {
  92.         --argc;
  93.         p = *targv++;
  94.         while (*++p != '\0') {
  95.             switch(mylower(*p)) {
  96.             case 'f':    /*  Alternate file name  */
  97.                 if (*++p == '\0') {
  98.                     --argc;
  99.                     if (targc-- == 0)
  100.                         usage();
  101.                     p = *targv++;
  102.                 }
  103.                 makefile = p;
  104.                 goto end_of_args;
  105. #ifdef eon
  106.             case 'm':    /*  Change space requirements  */
  107.                 if (*++p == '\0') {
  108.                     --argc;
  109.                     if (targc-- <= 0)
  110.                         usage();
  111.                     p = *targv++;
  112.                 }
  113.                 memspace = atoi(p);
  114.                 goto end_of_args;
  115. #endif
  116.             default :
  117.                 setoption(*p);
  118.                 break;
  119.             }
  120.         }
  121.     end_of_args:;
  122.     } else
  123.         *nargv++ = *targv++;
  124.   }
  125.  
  126.   /* evaluate and update environment MAKEFLAGS */
  127.   if((p =getenv("MAKEFLAGS")) != (char *)0)
  128.     while(*p) setoption(*p++);
  129.   for( p = ptrmakeflags; !isspace((int)*p); p++) ;
  130.   *p = '\0';
  131.   putenv(makeflags);
  132.  
  133.  
  134. #ifdef eon
  135.   if (initalloc(memspace) == 0xffff)  /*  Must get memory for alloc  */
  136.     fatal("Cannot initalloc memory",(char *)0,0);
  137. #endif
  138.  
  139.   if (makefile && strcmp(makefile, "-") == 0)  /*   use stdin as makefile  */
  140.     ifd = stdin;
  141.   else if (!makefile) {    /*  If no file, then use default */
  142.     if ((ifd = fopen(DEFN1, "r")) == (FILE *)0) {
  143.         if (errno != MNOENT || !DEFN2)
  144.             fatal("Can't open %s; error %02x", DEFN1, errno);
  145.         else if ((ifd = fopen(DEFN2, "r")) == (FILE *)0)
  146.             fatal("Can't open %s; error %02x", DEFN2, errno);
  147.     }
  148.   }
  149.   else if ((ifd = fopen(makefile, "r")) == (FILE *)0)
  150.     fatal("Can't open %s; error %2x", makefile, errno);
  151.  
  152.   init();
  153.  
  154.   makerules();
  155.  
  156.   mp = setmacro("MAKE", myname);
  157.   mp->m_flag |= M_MAKE;
  158.   setmacro("$", "$");
  159.  
  160.   /* set environment macros */
  161.   envp = environ; /* get actual environment ptr. */
  162.   while (*envp) {
  163.     if((p = strchr(*envp, '=')) != (char *)NULL) {
  164.         *p = '\0';
  165.         mp = setmacro(*envp, p + 1);
  166.         *p = '=';
  167.         if (useenv) mp->m_flag |= M_OVERRIDE;
  168.     } else
  169.         fatal("invalid environment: %s",*envp,0);
  170.  
  171.     ++envp;
  172.   }
  173.  
  174.   input(ifd);    /*  Input all the gunga  */
  175.   fclose(ifd);    /*  Finished with makefile  */
  176.   lineno = 0;    /*  Any calls to error now print no line number */
  177.  
  178.   if (print)
  179.     prt();    /*  Print out structures  */
  180.  
  181.   np = newname(".SILENT");
  182.   if (np->n_flag & N_TARG)  silent = TRUE;
  183.  
  184.   np = newname(".IGNORE");
  185.   if (np->n_flag & N_TARG)  ignore = TRUE;
  186.  
  187.   precious();
  188.  
  189.   if (!firstname)
  190.     fatal("No targets defined",(char *)0,0);
  191.  
  192.   circh();    /*  Check circles in target definitions  */
  193.  
  194.   if (!argc)
  195.     estat = make(firstname, 0);
  196.   else
  197.     while (argc--) {
  198.         estat |= make(newname(*argv++), 0);
  199.     }
  200.  
  201.   if (quest)
  202.     exit(estat);
  203.   else
  204.         exit(0);
  205. }
  206.  
  207. #ifdef __STDC__
  208. void setoption(char option)
  209. #else
  210. void setoption(option)
  211. char option;
  212. #endif
  213. {
  214.   register char *c;
  215.  
  216.   option = mylower(option);
  217.   switch(option) {
  218.     case 'n':    /*  Pretend mode  */
  219.         domake = FALSE;
  220.         break;
  221.     case 'i':    /*  Ignore fault mode  */
  222.         ignore = TRUE;
  223.         break;
  224.     case 'k':    /*  Continue on errror  */
  225.         conterr = TRUE;
  226.         break;
  227.     case 's':    /*  Silent about commands  */
  228.         silent = TRUE;
  229.         break;
  230.     case 'p':
  231.         print = TRUE;
  232.         break;
  233.     case 'r':
  234.         rules = FALSE;
  235.         break;
  236.     case 't':
  237.         dotouch = TRUE;
  238.         break;
  239.     case 'q':
  240.         quest = TRUE;
  241.         break;
  242.     case 'e':
  243.         useenv = TRUE;
  244.         break;
  245.     case 'd':
  246.         dbginfo = TRUE;
  247.         break;
  248.     case 'a':
  249.         ambigmac = TRUE;
  250.         break;
  251.     default:    /*  Wrong option  */
  252.         usage();
  253.   }
  254.   for( c = ptrmakeflags; !isspace((int)*c); c++)
  255.     if ( *c == option) return;
  256.   *c = option;
  257. }
  258.  
  259. void usage()
  260. {
  261.   fprintf(stderr, "Syntax: %s [{options | macro=val | target}]\n", myname);
  262.   fprintf(stderr, "Function: maintaining computer programs      V%s\n",version);
  263.   fprintf(stderr, "Options : -a : try to guess undefined ambiguous macros (*,<)\n");
  264.   fprintf(stderr, "          -d : print debugging information\n");
  265.   fprintf(stderr, "          -e : environment macro def. overwrite makefile def.\n");
  266.   fprintf(stderr, "          -f filename : makefile name (default: makefile, Makefile)\n");
  267.   fprintf(stderr, "          -i : ignore exit status of executed commands\n");
  268.   fprintf(stderr, "          -k : continue with unrelated branches on errors\n");
  269.   fprintf(stderr, "          -n : pretend to make\n");
  270.   fprintf(stderr, "          -p : print all macros & targets\n");
  271.   fprintf(stderr, "          -q : question up-to-dateness of target\n");
  272.   fprintf(stderr, "          -r : don't use inbuilt rules\n");
  273.   fprintf(stderr, "          -s : make silently\n");
  274.   fprintf(stderr, "          -t : touch files instead of making them\n");
  275.   fprintf(stderr, "Environment: MAKEFLAGS\n");
  276.   exit(1);
  277. }
  278.  
  279.  
  280. void fatal(msg, a1, a2)
  281. char *msg;
  282. char *a1;
  283. int   a2;
  284. {
  285.   fprintf(stderr, "%s: ", myname);
  286.   fprintf(stderr, msg, a1, a2);
  287.   fputc('\n', stderr);
  288.   exit(1);
  289. }
  290.